package com.zcsy.manage.web.main;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.github.pagehelper.PageHelper;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.constants.OwnerConstants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.util.JExcelReportUtil;
import com.zcsy.commons.util.JFreeChartUtil;
import com.zcsy.commons.util.MailUtil;
import com.zcsy.commons.util.PropertiesUtil;
import com.zcsy.commons.util.XlsImportUtils;
import com.zcsy.manage.model.base.CellHouse;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.base.OwnerHouse;
import com.zcsy.manage.model.base.ShopCategory;
import com.zcsy.manage.model.complain.ComplaintsInfo;
import com.zcsy.manage.model.mall.AdCategory;
import com.zcsy.manage.model.mall.AdInfo;
import com.zcsy.manage.model.mall.CouponInfo;
import com.zcsy.manage.model.mall.GoodsCollection;
import com.zcsy.manage.model.mall.GoodsInfo;
import com.zcsy.manage.model.mall.OrderInfo;
import com.zcsy.manage.model.news.CellManual;
import com.zcsy.manage.model.news.HelpManual;
import com.zcsy.manage.model.news.NoticeInfo;
import com.zcsy.manage.model.news.YellowPage;
import com.zcsy.manage.model.property.CarBill;
import com.zcsy.manage.model.system.Menu;
import com.zcsy.manage.model.system.Role;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.persistence.weixin.StringUtil;
import com.zcsy.manage.service.base.ICellHouseService;
import com.zcsy.manage.service.base.ICellInfoService;
import com.zcsy.manage.service.base.IOwnerHouseService;
import com.zcsy.manage.service.base.IShopCategoryService;
import com.zcsy.manage.service.common.CommonService;
import com.zcsy.manage.service.complain.IComplaintsInfoService;
import com.zcsy.manage.service.mall.IAdCategoryService;
import com.zcsy.manage.service.mall.IAdInfoService;
import com.zcsy.manage.service.mall.ICouponInfoService;
import com.zcsy.manage.service.mall.IGoodsCollectionService;
import com.zcsy.manage.service.mall.IGoodsInfoService;
import com.zcsy.manage.service.mall.IOrderInfoService;
import com.zcsy.manage.service.news.ICellManualService;
import com.zcsy.manage.service.news.IHelpManualService;
import com.zcsy.manage.service.news.INoticeInfoService;
import com.zcsy.manage.service.news.IYellowPageService;
import com.zcsy.manage.service.system.IDistrictInfoService;
import com.zcsy.manage.service.system.ILogService;
import com.zcsy.manage.service.system.IMenuService;
import com.zcsy.manage.service.system.IRoleService;
import com.zcsy.manage.service.system.IUserService;

/**
 * 
 * @Title: MainController.java
 * @Package： com.zcsy.manage.web.main 
 * @Description: 工程入口方法
 * @author chensf 735569870@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年1月8日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
public class MainController{
    
    @Autowired
    private IOwnerHouseService ownerHouseService;
    
	@Autowired
	private IMenuService menuService;
	
	@Autowired 
	private ILogService logService;
	
	@Autowired 
	private IRoleService roleService;
	
	@Autowired 
	private ICellHouseService cellHouseService;
	
	@Autowired 
	private IAdCategoryService adCategoryService;
	
	@Autowired 
	private IAdInfoService adInfoService;
	
	@Autowired 
	private IGoodsInfoService goodsInfoService;

	@Autowired
	private INoticeInfoService noticeInfoService;
	
	@Autowired
	private IYellowPageService yellowPageService;
	
    @Autowired
    private IUserService userService;
    
    @Autowired
    private ICouponInfoService couponInfoService;
    
    @Autowired
    private ICellManualService cellManualService;

    @Autowired
    private IDistrictInfoService districtInfoService;
    
    @Autowired
    private IShopCategoryService shopCategoryService;
    
    @Autowired
    private ICellInfoService cellInfoService;
    
    @Autowired
    private IHelpManualService helpManualService;
    
    @Autowired
    private IComplaintsInfoService infoService;
    
    @Autowired
	private IOrderInfoService orderInfoService;
    
    @Autowired
    private IGoodsCollectionService goodsCollectionService;
    
    private final static int repairType = 2;//帮助手册-物业报修类型
    
    /**
     * @description 获取登录前商户入驻的店铺分类信息
     * @return 返回到   登陆首页--我要开店
     * @author chenxin
     * @date 2016年8月11日上午11:20:38
     */
    @RequestMapping(value="" , method = RequestMethod.GET)
    public String toLogin(Model model, HttpServletRequest req,HttpServletResponse res){
        
        List<ShopCategory> shopCategories = shopCategoryService.getParentBylevel((int)Constants.MENU_LEVEL_FIREST);
        model.addAttribute("provinceList",districtInfoService.getDistrictInfoByLevel((int)Constants.MENU_LEVEL_FIREST));
        model.addAttribute("shopList",shopCategories);

        // 商户登陆地址
        String url = PropertiesUtil.getPropertiesUtilValues(PropertiesUtil.SELLER_LOGIN_RUL);
        model.addAttribute("sellerUrl",url);
        
        return "login";
    }
    
    /**
     * 二三级菜单区域加载失败
     * 
     * @param model	对象
     * @param req	请求
     * @param res	响应
     * @param menuId	菜单ID
     * @return
     */
    @RequestMapping(value="leftError" , method = RequestMethod.GET)
    public String leftError(Model model, HttpServletRequest req,HttpServletResponse res,
    		int menuId){
    	model.addAttribute("menuId", menuId);
    	return "error/leftError";
    }
    
    /**
     * 内容区域加载失败
     * 
     * @param model	对象
     * @param req	请求
     * @param res	响应
     * @param url	内容链接
     * @return
     */
    @RequestMapping(value="contentError" , method = RequestMethod.GET)
    public String contentError(Model model, HttpServletRequest req,HttpServletResponse res,
    		String url){
    	model.addAttribute("url", url);
    	return "error/contentError";
    }
    
    
    /**
     * 登陆后界面跳转
     * @Method: main
     * @param model
     * @param item 界面标识
     * @author hubing
     * @date 2016年7月28日
     */
    @RequestMapping(value = "main", method = RequestMethod.GET)
    public String main(Model model, Integer item, HttpServletRequest request,
            HttpServletResponse response) throws Exception{
    	
    	final HttpSession session = request.getSession();
    	
        // 获取当前用户
        User user = CommonService.getSessionUser();
        if (user == null) {
            response.sendRedirect(request.getContextPath() + "/");
            return null;
        }
        String pageUrl = "";
        switch (item) {
        case Constants.MAIN_ITEM_HOME:
        	mainPage(model, request);
            pageUrl = "main";
            break;
        case Constants.MAIN_ITEM_INFO:
        	infoMain(model, request);
            pageUrl = "main/main_information";
            break;
        case Constants.MAIN_ITEM_PROY:
            goToRepairUsualQuestionsPage(model,request);
        	getPropertyPayBillInfo(model,request,response);
        	pageUrl = "main/main_property";
            break;
        case Constants.MAIN_ITEM_MALL:
        	getMainHomePage(model,request);
            pageUrl = "main/main_mall";
            break;
        case Constants.MAIN_ITEM_SUGN:
            getSuggestionEditInfo(model,request);
            pageUrl = "main/main_suggestion";
            break;
        case Constants.MAIN_ITEM_PERL:
            getUserInfo(model,request);
            pageUrl = "main/main_personal";
            break;
        default:
            break;
        }
        
        session.setAttribute(RequestConstants.ITEM_MAIN, Constants.ITEM_MAIN);
        session.setAttribute(RequestConstants.ITEM_INFORMATION, Constants.ITEM_INFORMATION);
        session.setAttribute(RequestConstants.ITEM_PROPERTY, Constants.ITEM_PROPERTY);
        session.setAttribute(RequestConstants.ITEM_MALL, Constants.ITEM_MALL);
        session.setAttribute(RequestConstants.ITEM_SUGGESTION, Constants.ITEM_SUGGESTION);
        session.setAttribute(RequestConstants.ITEM_PERSONAL, Constants.ITEM_PERSONAL);
        
        model.addAttribute("item", item);
        return pageUrl;
    }
    
    /**
     * @Description 进入首页
     * @author liuying 1075817474@qq.com
     * @date 2016年8月10日
     * @see 
     */
    public void mainPage(Model model, HttpServletRequest request){
        String cellId = ((CellInfo) request.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
    	//我要开店-业主小区信息
    	CellInfo cellInfo  = cellInfoService.selectByPrimaryKey(cellId);

    	model.addAttribute("provinceList",districtInfoService.getDistrictInfoByLevel((int)Constants.MENU_LEVEL_FIREST));
    	model.addAttribute("cellInfo", cellInfo);
    	List<ShopCategory> shopCategories = shopCategoryService.getParentBylevel((int)Constants.MENU_LEVEL_FIREST);
        model.addAttribute("shopList",shopCategories);
    	//根据小区id查询最新5条通知信息
    	List<NoticeInfo> noticeInfos = noticeInfoService.getNewNoticeList(cellId, Constants.SHOW_FIVE);
    	
    	//根据分类值获取当前业主首页广告分类
    	AdCategory adCategory =  adCategoryService.selectByValue("ownerindex");
    	
    	if (adCategory != null) {
    		//根据广告位分类id查询有效广告位信息
        	List<AdInfo> adInfoList = adInfoService.selectBannerByAdCategoryId(adCategory.getId());
        	if (adInfoList != null && adInfoList.size() > 0) {
        		model.addAttribute("adInfos", adInfoList);
        		model.addAttribute("bannerSize", adInfoList.size());
			} else {
				model.addAttribute("defaultImg", adCategory.getDefaultImg());
			}
		}
    	
    	//秒杀商品
    	List<GoodsInfo> seckillGoodsList = goodsInfoService.getSecKillGoodsList();
    	model.addAttribute("seckillGoodsList",seckillGoodsList);
    	model.addAttribute("bannerSizeSeckill", seckillGoodsList.size());
    	
    	/*//根据分类值获取当前业主首页广告分类
    	AdCategory adCategorySeckill =  adCategoryService.selectByValue("seckill");
    	
    	if (adCategorySeckill != null) {
    		//根据广告位分类id查询有效广告位信息
        	List<AdInfo> adInfoListSeckill = adInfoService.selectBannerByAdCategoryId(adCategorySeckill.getId());
    		model.addAttribute("adInfosSeckill", adInfoListSeckill);
    		model.addAttribute("bannerSizeSeckill", adInfoListSeckill.size());
     		model.addAttribute("defaultImgSeckill", adCategorySeckill.getDefaultImg());
		}*/
    	model.addAttribute("noticeInfos",noticeInfos);
    }

    /**
     * @Description 个人信息首页进入
     * @author yangfan 373188993@qq.com
     * @date 2016年8月5日
     * @see 
     */
    /**
     * @Description:添加进入个人中心首页时 ，右侧为完成功能
     * @author huangls 1449700087@qq.com
     * @date 2016年8月19日
     */
    public void getUserInfo(Model model, HttpServletRequest request){
    	
    	//获取当前登陆的用户的信息
        User user = (User)request.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        String userAccount = user.getLoginname();
        User userInfo = userService.selectByLoginName(userAccount);
        
        //获取到当前用户登陆时默认的小区id
        String cellId = ((CellInfo) request.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
        
		// 业主当前小区的待付款订单
		List<OrderInfo> waitPayMoneyOrderList = orderInfoService.selectOrderInfoList(cellId,userAccount, Constants.WAIT_PAY_MONEY);
		
		// 业主当前小区的待收货订单
		List<OrderInfo> waitReceiveGoodsOrderList = orderInfoService.selectOrderInfoList(cellId,userAccount, Constants.WAIT_RECEIVE_GOODS);
		
		// 业主当前小区的待评价订单
		List<OrderInfo> waitCommentOrderList = orderInfoService.selectOrderInfoList(cellId,userAccount, Constants.WAIT_COMMENT);
		
		// 业主当前小区的待发货订单
		List<OrderInfo> waitSendGoodsOrderList = orderInfoService.selectOrderInfoList(cellId,userAccount, Constants.WAIT_SEND_GOODS);
		
		// 业主当前小区的所有订单信息，以及订单详情信息和订单详情下的商品信息
		List<OrderInfo> orderInfoListAll = orderInfoService.selectOrderInfoByCellIdStateUserAccount(cellId,userAccount, Constants.ALL_ORDER);
		
        //仅查询订单信息 ，用于分页
        List<OrderInfo> orderInfoList = orderInfoService.selectOrderAllInfoList(cellId, userAccount);
        for( int i = 0; i < orderInfoList.size(); i++) {
        	for(int j = 0; j < orderInfoListAll.size(); j++){
        	    if(Constants.ORDER_LEVEL_ONE == orderInfoList.get(i).getOrderLevel()) {
                    if(orderInfoList.get(i).getId().equals(orderInfoListAll.get(j).getParentId())) {
                        
                        if(null == orderInfoList.get(i).getOrderDetailList()) {
                            orderInfoList.get(i).setOrderDetailList(orderInfoListAll.get(j).getOrderDetailList());
                        } else {
                            for(int k = 0; k < orderInfoListAll.get(j).getOrderDetailList().size(); k++) {
                                orderInfoList.get(i).getOrderDetailList().add(orderInfoListAll.get(j).getOrderDetailList().get(k));
                            }
                        }
                    }
                } else {
                    //将orderInfoListAll对应订单的订单详情信息插入到orderInfoList中
                    if(orderInfoList.get(i).getId().equals(orderInfoListAll.get(j).getId())) {
                        if(null == orderInfoList.get(i).getOrderDetailList()) {
                            orderInfoList.get(i).setOrderDetailList(orderInfoListAll.get(j).getOrderDetailList());
                        } else {
                            
                            for(int k = 0; k < orderInfoListAll.get(j).getOrderDetailList().size(); k++) {
                                orderInfoList.get(i).getOrderDetailList().add(orderInfoListAll.get(j).getOrderDetailList().get(k));
                            }
                        }
                    }
                }
        	}
        	
        }
		
		
		
		// 根据业主id联表查询收藏列表
        List<GoodsCollection> myCollection = goodsCollectionService.getGoodsCollectionByOwnerId(userInfo.getId());
		
		//查询可使用的优惠券
		CouponInfo couponInfo = new CouponInfo();
        couponInfo.setUserAccount(userAccount);
        List<CouponInfo> couponInfoList = couponInfoService.selectByUserAccountAndState(couponInfo);
        
		model.addAttribute("couponInfoList",  couponInfoList);
		model.addAttribute("waitPayMoneyOrderList", waitPayMoneyOrderList);
		model.addAttribute("waitReceiveGoodsOrderList", waitReceiveGoodsOrderList);
		model.addAttribute("waitCommentOrderList", waitCommentOrderList);
		model.addAttribute("waitSendGoodsOrderList", waitSendGoodsOrderList);
		model.addAttribute("orderInfoListAll", orderInfoList);
		model.addAttribute("myCollection", myCollection);
        model.addAttribute("userInfo",userInfo);
    }
        
    
    /**
     * @Description 资讯首页进入
     * @author liuying 1075817474@qq.com
     * @date 2016年7月29日
     * @see 
     */
    public void infoMain(Model model, HttpServletRequest request){
    	
        String cellId = ((CellInfo) request.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
    	
    	//根据小区id查询最新7条通知信息
    	List<NoticeInfo> noticeInfos = noticeInfoService.getNewNoticeList(cellId, Constants.SHOW_SEVEN);
    	
    	//根据小区id查询最新6条社区服务信息
    	List<CellManual> cellManuals = cellManualService.getSomeListBySort(cellId, Constants.SHOW_SIX);
    	
    	//根据小区id查询最新6条便民黄页信息
    	List<YellowPage> yellowPages = yellowPageService.getSomeListBySort(cellId, Constants.SHOW_SIX);
    	
    	model.addAttribute("noticeInfos",noticeInfos);
    	model.addAttribute("cellManuals",cellManuals);
    	model.addAttribute("yellowPages",yellowPages);
    	
    }
    
    public void getMainHomePage(Model model, HttpServletRequest req) {
    	AdCategory adCategory = adCategoryService.selectByValue("goodsbanner");
    	if (adCategory != null) {
    		List<AdInfo> adInfos = adInfoService.selectBannerByAdCategoryId(adCategory.getId());
    		if (adInfos != null && adInfos.size() > 0) {
    			model.addAttribute("adInfos", adInfos);
    			model.addAttribute("bannerSize", adInfos.size());
    			
			} else {
				model.addAttribute("defaultImg", adCategory.getDefaultImg());
			}
		} 
    	
    	//获取平台优惠券
        String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
        List<CouponInfo> couponInfoList = couponInfoService.getflatCouponInfo();
        model.addAttribute("couponInfos", couponInfoList);
        
    	//秒杀专区
        List<GoodsInfo> seckillGoodsList = goodsInfoService.getSecKillGoodsList();
    	model.addAttribute("seckillGoodsList",seckillGoodsList);
    	model.addAttribute("bannerSizeSeckill", seckillGoodsList.size());
		
		//左边精品
		AdCategory adLeft = adCategoryService.selectByValue("primeGoods");
		if (adLeft != null) {
    		List<AdInfo> adGoodInfos = adInfoService.selectByAdCategoryId(adLeft.getId(),cellId);
    		model.addAttribute("adGoodInfos", adGoodInfos);
    		model.addAttribute("adGoodInfosSize", adGoodInfos.size());
    		model.addAttribute("defaultImgAdLeft", adLeft.getDefaultImg());
		}
		//右边精品
		AdCategory adRight = adCategoryService.selectByValue("primeGoods1");
		if (adRight != null) {
			List<AdInfo> adGoodInfos = adInfoService.selectByAdCategoryId(adRight.getId(),cellId);
			model.addAttribute("adGoodInfos1", adGoodInfos);
    		model.addAttribute("adGoodInfos1Size", adGoodInfos.size());
    		model.addAttribute("defaultImgAdRight", adRight.getDefaultImg());
		}
		//下边精品
		AdCategory adDown = adCategoryService.selectByValue("primeGoods2");
		if (adDown != null) {
			List<AdInfo> adGoodInfos = adInfoService.selectByAdCategoryId(adDown.getId(),cellId);
			model.addAttribute("adGoodInfos2", adGoodInfos);
    		model.addAttribute("adGoodInfos2Size", adGoodInfos.size());
    		model.addAttribute("defaultImgAdDown", adDown.getDefaultImg());
		}
		
		//热门店铺
		AdCategory hotShop = adCategoryService.selectByValue("hotShops");
		if (hotShop != null) {
    		List<AdInfo> adHotShopInfos = adInfoService.selectByAdCategoryId(hotShop.getId(),cellId);
    		model.addAttribute("adHotShopInfos", adHotShopInfos);
    		model.addAttribute("adHotShopInfosSize", adHotShopInfos.size());
    		model.addAttribute("defaultImgHotShop", hotShop.getDefaultImg());
		}
	}
    
	public void getPropertyPayBillInfo(Model model,final HttpServletRequest req,
			HttpServletResponse response) throws Exception{
        User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        String userID = user.getId();
		// 获取业主房屋信息
		List<CellHouse> houses = cellHouseService
				.getListByOwnerIdAndState(userID, OwnerConstants.NORMAL_HOUSE_STATE);
		for (CellHouse cellHouse : houses) {
			cellHouse.setFullName(cellHouse.getCellName()+cellHouse.getRegionName()+cellHouse.getBuildingName()
					+cellHouse.getUnitName()+cellHouse.getName());
		}
		List<CarBill> cars = cellHouseService.getOwnerAllCars(userID);
		model.addAttribute("cars", cars);
		model.addAttribute("houses", houses);
	}
	
    /**
     * 二三级菜单区域刷新
     * 
     * @param menuId 一级菜单ID
     * @param req 请求
     * @param res 响应
     * @return
     * @throws Exception
     */
    @RequestMapping(value="left" , method = RequestMethod.GET)
	public String left(String menuId,
			HttpServletRequest req, HttpServletResponse res) throws Exception{
    	// 获取session
		HttpSession session = req.getSession();

		// 获取当前用户
		User user = CommonService.getSessionUser();

		if (user == null) {
			res.sendRedirect(req.getContextPath() + "/");
			return null;
		}
		
		//获取菜单
		List<Menu> menus = CommonService.getSessionMenus();
		List<Menu> secondMenus = null;
		for (Menu menu : menus) {
			if (menu.getId().equals(menuId)) {
				secondMenus = menu.getSubMenus();
				break;
			}
		}
		
		session.setAttribute("secondMenus", secondMenus);
    	return "left";
    }
    
    /**
     * 获取一级菜单下第一个内容菜单刷新显示
     * 
     * @param menuId  一级菜单ID
     * @return
     * @throws Exception
     */
    @RequestMapping(value="content" , method = RequestMethod.GET)
    @ResponseBody
	public RequestJson content(String menuId) throws Exception{
    	RequestJson json = new RequestJson();
		try{

		// 从缓存中获取菜单
		List<Menu> menus = CommonService.getSessionMenus();
		List<Menu> secondMenus = null;
		for (Menu menu : menus) {
			if (menu.getId().equals(menuId)) {
				secondMenus = menu.getSubMenus();
				break;
			}
		}
		
		Menu homeMenu = menuService.getFirstAvailableMenus(secondMenus);
		json.setObj(homeMenu);
		}catch(Exception e){
			json.setSuccess(false);
		}
    	return json;
    }
    
    @RequestMapping(value="changeCell" , method = RequestMethod.GET)
    @ResponseBody
	public RequestJson changeCell(String cellId) throws Exception{
    	RequestJson json = new RequestJson();
		try{
			// 获取当前用户
			User user = CommonService.getSessionUser();
			if(user.getUserType() != Constants.ADMIN_TYPE){
				if(!menuService.updateUserFirstMenuBycell(user, Constants.SYSTEM_OPERATION_WEB, cellId)){
					json.setSuccess(false);
					json.setMsg("您没有该小区的管理权限，请联系管理员");
				}else{
					CommonService.setSessionUser(user);
					CommonService.setSessionMenus(user.getCellMenus());
				}
			}else{
				if(!menuService.updateAdminUserFirstMenu(user, Constants.SYSTEM_OPERATION_WEB, cellId)){
					json.setSuccess(false);
					json.setMsg("您没有该小区的管理权限，请联系管理员");
				}else{
					CommonService.setSessionUser(user);
					CommonService.setSessionMenus(user.getCellMenus());
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			json.setSuccess(false);
			json.setMsg("切换失败");
		}
    	return json;
    }
    
    @RequestMapping(value="searchCell" , method = RequestMethod.GET)
    @ResponseBody
	public RequestJson searchCell(String cellName) throws Exception{
    	RequestJson json = new RequestJson();
		try{
			// 获取当前用户
			User user = CommonService.getSessionUser();
			if(StringUtils.isEmpty(cellName)){
				json.setObj(user.getCellInfos());
				return json;
			}
			
			List<CellInfo> cells = new ArrayList<CellInfo>();
			if(user.getCellInfos() != null && user.getCellInfos().size() > 0){
				for(CellInfo item : user.getCellInfos()){
					if(item.getCellName().contains(cellName)){
						cells.add(item);
					}
				}
			}
			
			json.setObj(cells);
			return json;
		}catch(Exception e){
			json.setSuccess(false);
		}
    	return json;
    }
	
	/**
	 * 
	 * @Description <br>
	 * 测试发送邮件方法
	 * @param content 邮件正文
	 * @param subject 邮件主题
	 * @param req 请求
	 * @return 发送结果
	 * @author chensf 735569870@qq.com
	 * @date 2016年1月11日
	 * @see
	 */
	@RequestMapping(value="sendEmail" , method = RequestMethod.GET)
    @ResponseBody
    public RequestJson saveOrUpdateEmail(String content,String subject, HttpServletRequest req){
	    
        boolean sendResult = false;
        RequestJson result = new RequestJson();
        
        try {
            
            //邮箱数组，只能用次方式获取参数
            String[] emails = req.getParameterValues("emails");
            
            sendResult = MailUtil.sendEmail(emails, content,subject);
                
            if (sendResult) {
                result.setMsg("发送成功");// 保存状态到前端
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setMsg("发送失败");
            }
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("操作失败");
        }
        return result;
    }
	
	/**
	 * 
	 * @Description <br>
	 * 柱状图报表测试方法
	 * @param model 
	 * @param req 请求
	 * @return 页面路径
	 * @author chensf 735569870@qq.com
	 * @date 2016年1月12日
	 * @see
	 */
	@RequestMapping(value="getBarReport" , method = RequestMethod.GET)
    public String getBarReport(Model model,HttpServletRequest req){
	    
	    //获取session
	    HttpSession session = req.getSession();
	    
	    ArrayList<ArrayList<String>> dataList = new ArrayList<ArrayList<String>>();
	    
	    List<Map<String, Object>> logReports = logService.getReportByDate();
	    
	    //将要生成报表的数据放到list集合
	    for(Map<String, Object> item : logReports){
	        
	        ArrayList<String> list = new ArrayList<String>();
	        
	        list.add(item.get("amount").toString());
	        list.add(item.get("logcontent").toString());
	        list.add(item.get("date").toString().substring(5));
	        
	        dataList.add(list);
	    }
	    
	    //报表标题
	    String title = "日志统计";
	    //x轴标题
	    String xTitle = "操作类型";
	    //y轴标题
	    String yTitle = "数量";
	    //报表是否垂直显示
	    boolean isVertical = true;
	    //是否使用legend
	    boolean useLegend = true;
	    boolean useTooltip = true;
	    boolean useUrl = false;
	    int width = 1500;
	    int height = 400;
	    //是否使用3D
	    boolean is3D = false;
	    //是否显示值
        boolean showValue = true;
	    
	    Map<String, String> map = JFreeChartUtil.createBarReport(dataList, title, xTitle, yTitle, 
	        isVertical, useLegend, useTooltip, useUrl,width,height,is3D,showValue, session, req);
	    
	    model.addAttribute("graphURL", map.get("graphURL"));
	    model.addAttribute("filename", map.get("filename"));
	    
        return "system/report/logReport";
        
    }
	
	/**
	 * @Description 附件管理页面<br>
	 * @param model 视图对象
	 * @param req 请求对象
	 * @return 页面地址
	 * @author tanhuatang 786468449@qq.com
	 * @date 2016年1月15日
	 * @see
	 */
	@RequestMapping(value="filePage" , method = RequestMethod.GET)
	public String filePage(Model model,HttpServletRequest req) {
	    return "system/uploader/filePage";
	}

	/**
     * 
     * @Description <br>
     * 柱状图报表测试方法
     * @param model 
     * @param req 请求
     * @return 页面路径
     * @author chensf 735569870@qq.com
     * @date 2016年1月12日
     * @see
     */
    @RequestMapping(value="getPieReport" , method = RequestMethod.GET)
    public String getPieReport(Model model,HttpServletRequest req){
        
        //获取session
        HttpSession session = req.getSession();
        
        ArrayList<ArrayList<String>> dataList = new ArrayList<ArrayList<String>>();
        
        List<Map<String, Object>> logReports = logService.getReport();
        
        //将要生成报表的数据放到list集合
        for(Map<String, Object> item : logReports){
            
            ArrayList<String> list = new ArrayList<String>();
            
            list.add(item.get("logcontent").toString());
            list.add(item.get("amount").toString());
            
            dataList.add(list);
        }
        
        //报表标题
        String title = "日志统计";
        
        //是否使用legend
        boolean useLegend = true;
        boolean useTooltip = true;
        boolean useUrl = false;
        int width = 700;
        int height = 400;
        //是否使用3D
        boolean is3D = true;
        //是否3D透明
        boolean set3Dstyle = false;
        //s是否圆形
        boolean isCircular = false;
        //是否显示百分比
        boolean showPercent = true;
        
        Map<String, String> map = JFreeChartUtil.createPieChart(dataList, title,set3Dstyle,isCircular,
             useLegend, useTooltip, useUrl,width,height,is3D,showPercent, session, req);
        
        model.addAttribute("graphURL", map.get("graphURL"));
        model.addAttribute("filename", map.get("filename"));
        
        return "system/report/logReport";
        
    }
    
    /**
     * 
     * @Description <br>
     * 折线图报表测试方法
     * @param model 
     * @param req 请求
     * @return 页面路径
     * @author chensf 735569870@qq.com
     * @date 2016年1月14日
     * @see
     */
    @RequestMapping(value="getLineReport" , method = RequestMethod.GET)
    public String getLineReport(Model model,HttpServletRequest req){
        //报表标题
        String title = "日志统计";
        
        //x轴标题
        String xTitle = "";
        //y轴标题
        String yTitle = "数量";
        
        //获取session
        HttpSession session = req.getSession();
        
        ArrayList<ArrayList<String>> dataList = new ArrayList<ArrayList<String>>();
        
        //按天统计方式
        /*List<Map<String, Object>> logReports = logService.getReportByDate();
        
        Set<String> sets = new HashSet<String>();
        //将要生成报表的数据放到list集合
        for(Map<String, Object> item : logReports){
            
            sets.add(item.get("date").toString().substring(0,4));
            
            ArrayList<String> list = new ArrayList<String>();
            
            list.add(item.get("amount").toString());
            list.add(item.get("logcontent").toString());
            list.add(item.get("date").toString().substring(5));
            
            dataList.add(list);
        }
        
        for(String set : sets){
            if("".equals(xTitle)){
                
                xTitle = set;
            }else{
                
                xTitle += "-" + set;
            }
        }*/
        
        //按周统计方式
        /*List<Map<String, Object>> logReports = logService.getReportByWeek();
        
        //将要生成报表的数据放到list集合
        for(Map<String, Object> item : logReports){
            
            ArrayList<String> list = new ArrayList<String>();
            
            list.add(item.get("amount").toString());
            list.add(item.get("logcontent").toString());
            list.add(item.get("year").toString() + "第" + item.get("week").toString() + "周");
            
            dataList.add(list);
        }*/
        
        //按月统计方式
        List<Map<String, Object>> logReports = logService.getReportByMonth();
        
        //将要生成报表的数据放到list集合
        for(Map<String, Object> item : logReports){
            
            ArrayList<String> list = new ArrayList<String>();
            
            list.add(item.get("amount").toString());
            list.add(item.get("logcontent").toString());
            list.add(item.get("year").toString() + "年" + item.get("month").toString() + "月");
            
            dataList.add(list);
        }
        
        //是否使用legend
        boolean useLegend = true;
        boolean useTooltip = true;
        boolean useUrl = false;
        int width = 1000;
        int height = 500;
        //是否使用3D
        boolean is3D = false;
        //是否显示值
        boolean showValue = false;
        
        Map<String, String> map = JFreeChartUtil.createLineReport(dataList, title, xTitle, yTitle, 
            useLegend, useTooltip, useUrl,width,height,is3D,showValue, session, req);
        
        model.addAttribute("graphURL", map.get("graphURL"));
        model.addAttribute("filename", map.get("filename"));
        
        return "system/report/logReport";
        
    }
    
    /**
     * 
     * @Description <br>
     * 导出excel测试方法
     * @param request 请求
     * @param response 响应
     * @author chensf 735569870@qq.com
     * @date 2016年1月15日
     * @see
     */
    @RequestMapping(value="exportLog" , method = RequestMethod.GET)
    @ResponseBody
    public void exportLog( HttpServletRequest request,HttpServletResponse response){
        
        List<Map<String, Object>> logReports = logService.getReportByMonth();
        ArrayList<ArrayList<String>> dataList = new ArrayList<ArrayList<String>>();
        
        for(Map<String, Object> log : logReports){
            ArrayList<String> reportList = new ArrayList<String>();
            
            reportList.add(log.get("year").toString() + "年" + log.get("month").toString() + "月");
            reportList.add(log.get("logcontent").toString());
            reportList.add(log.get("amount").toString());
            
            dataList.add(reportList);
        }
        
        // 导出标题列
        String[] rows = new String[] {"日期","操作内容", "数量"};
        // 导出excel文件每列宽度
        int[] style = new int[] {20, 20, 20};
        // Excel文件sheet名称
        String sheetName = "日志报表统计";

        try {
            ServletOutputStream fout = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "filename=order_report.xls");
            JExcelReportUtil jexcelOutStream = new JExcelReportUtil();
            jexcelOutStream.excelreport(sheetName, dataList, rows, fout, style);
            fout.flush();
            fout.close();
        } catch (Exception e) {}
        
    }
    
    /**
     * 
     * @Description <br>
     * excel导入测试方法
     * @param request 请求
     * @param response 响应
     * @return 导入结果
     * @author chensf 735569870@qq.com
     * @date 2016年1月15日
     * @see
     */
    @RequestMapping(value="importXls" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson importXls(HttpServletRequest request,HttpServletResponse response) {

        RequestJson json = new RequestJson();
        
      //文件输入流
        InputStream inputStream = null;

        if (ServletFileUpload.isMultipartContent(request)) {
            try {
                //转换为文件请求
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                //获取多文件
                Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();

                //文件遍历上传
                for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                    MultipartFile file = entity.getValue();
                    if (file != null && file.getSize() > 0) {
                        
                        //获取文件输入流
                        inputStream = file.getInputStream();

                        List<List> list = XlsImportUtils.parseXlsToString(inputStream, 2, 1, 0);
                        if (list != null && !list.isEmpty()) {

                            for(List item : list){

                                Role role = new Role();
                                role.setRoleName(item.get(0).toString());
                                role.setDescrip(item.get(1).toString());

                                roleService.insert(role);
                            }
                            json.setSuccess(true);
                        } else {
                            json.setSuccess(false);
                            json.setMsg("文件数据错误！");
                        }
                    }
                }
            } catch (Exception e) {
                json.setSuccess(false);
                json.setMsg("操作异常！");
                
                e.printStackTrace();
            }finally {
                
                if (inputStream != null) {
                    
                    try {
                        
                        inputStream.close();
                    } catch (IOException e) {
                        
                        e.printStackTrace();
                    }
                }
            }
        }

        return json;
    }

    private final static int LEVEL_five = 5;

    public String getSuggestionEditInfo(Model model,HttpServletRequest req) {
        try {
          //获取到当前用户登陆时默认的小区id
            String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
            //获取当前登陆的用户的信息
            User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
            
            String ownerId = user.getId();
            OwnerHouse ownerHouse = new OwnerHouse();
            List<CellHouse> cellHouseList = ownerHouseService.getCurrentCellHouse(ownerId, cellId);
            
            if(! StringUtils.isEmpty(ownerId)){
                ownerHouse.setOwnerId(ownerId);
            }
            
            
            model.addAttribute("cellHouseList", cellHouseList);
            model.addAttribute("cellId", cellId);
            model.addAttribute("level", LEVEL_five);
         
        } catch (Exception e) {
        }
        return "main/main_suggestion";
    }
    /**
     * @description 
     * @author chenxin
     * @date 2016年8月16日下午1:48:24
     */
    public void goToRepairUsualQuestionsPage(Model model, HttpServletRequest request) {
        
        HelpManual helpManual = new HelpManual();
        String cellId = ((CellInfo) request.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
        helpManual.setCellId(cellId);
        helpManual.setType(repairType);
        List<HelpManual> helpManuals = helpManualService.selectByKeyword(helpManual);
        
        User user = (User) request.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        List<ComplaintsInfo> InfoList = infoService.getAllPropertySuggestion(user.getId());
        model.addAttribute("helpManuals", helpManuals);
        model.addAttribute("complaintsInfos", InfoList);
    }
}
